<?php slot("page", "update"); ?>

<!--h1>Updates</h1-->

<?php
    if ($sf_user->hasFlash("updateInfo")) {
        echo "<div class='alert alert-success'>" . $sf_user->getFlash("updateInfo") . "</div>";
    }
    
    if (count($runs) == 0) {
        echo "<div class='alert alert-info'><strong>This page shows the history of updates for your sources</strong>, but you currently have no sources configured. Head to the <a href='" . url_for("feedback/setup") . "'>setup</a> page to add your apps.</div>";
    }
    else {
?>
    
    <?php if (!$hideDescriptionMessage): ?>
        <div class="alert alert-info" id="descriptionMessage">
            <a class="close" href="#" title="Hide this message">&times;</a>
            In order for <strong>Fydal</strong> to help you manage the feedback from your apps, your sources need to be "updated". This is the process of importing all feedback into
            <strong>Fydal</strong>'s database for easy display, organising & graphing. You can request an on-demand update, or otherwise your sources will be regularly updated. Updates
            can take anywhere between 10 seconds and an hour, depending on the amount of outstanding feedback to import. The first update for a source is typically the slowest.
        </div>
        <script type="text/javascript">
            $("#descriptionMessage .close").click(function() {
                $("#descriptionMessage").alert("close");
                document.cookie = "hideDescriptionMessage=true; path=/";
            });
        </script>
    <?php endif; ?>

    <p>
        <a href="<?php echo url_for('feedback/update') ?>"><button class="btn">Refresh</button></a> 
        <a href="<?php echo url_for('feedback/doRequestUpdate?sourceId=all') ?>"><button class="btn"><i class="icon-refresh"></i> Request updates for all sources</button></a>
    </p>
    
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Source</th>
                <th>Status</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php
            
                foreach ($runs as $run) {
                    $status = "";
                    $date = "";
                    $showUpdateButton = false;
                    
                    if (strtotime($run->getEndDate()) > 0) {
                        $status = "Complete (" . ($run->getFeedbackCount() > 0 ? $run->getFeedbackCount() : "no") . " item(s) added)";
                        $date = DateHelper::fuzzyDateDetailed($run->getEndDate()) . " (took " . (strtotime($run->getEndDate()) - strtotime($run->getStartDate())) . " seconds)";
                        $showUpdateButton = true;
                    }
                    else if (strtotime($run->getEndDate()) == 0 && strtotime($run->getStartDate()) > 0) {
                        $status = "<img src='/img/loading.gif' /> Running now, please be patient...";
                        $date = DateHelper::fuzzyDateDetailed($run->getStartDate());
                    }
                    else if (strtotime($run->getRequestedDate()) > 0) {
                        $status = "Requested";
                        $date = DateHelper::fuzzyDateDetailed($run->getRequestedDate());
                    }
                    
                    ?>
                        <tr>
                            <td class="update-source">
                                <?php echo DisplayHelper::printIconWithRoundedCorners($run->Source->getFinalIconUrl()) ?>
                                
                                <?php
                                    $sourceName = "";
                                    $userSource = $userSourcesById[$run->Source->getId()];
                                    if ($userSource) {
                                        $sourceName = $userSource->getDisplayName();
                                    }
                                    else {
                                        $sourceName = $run->Source->getName();
                                    }
                                ?>
                                <div><strong><?php echo $sourceName ?>  <?php echo DisplayHelper::createProviderIconHtml($run->Source->getProvider()) ?></strong></div>
                                
                                <div>
                                    <a href="<?php echo url_for('feedback/list') . '?sourceId=' . $run->Source->getId() ?>"><button class="btn">View feedback</button></a>
                                    <?php if ($showUpdateButton): ?>
                                        <a href="<?php echo url_for('feedback/doRequestUpdate?sourceId=' . $run->Source->getId()) ?>"><button <?php if ($run->Source->getRunStatus() != Source::RUN_STATUS_NONE) echo "disabled" ?> class="btn"><i class="icon-refresh"></i> Update again</button></a>
                                    <?php endif; ?>
                                </div>
                            </td>
                            <td>
                                <?php echo $status; ?>
                            </td>
                            <td>
                                <?php echo $date; ?>
                            </td>
                        </tr>
                    <?php
                }
            
            ?>
        </tbody>
    </table>
<?php
    }
?>

